﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
public partial class GateService_registerManger : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Form["isexemail"] != null)
            {
                IsEmialExists(Request.Form["isexemail"], Request.Form["langid"]);
            }
            
            if (Request.Form["getArts"]!=null)
             {
                    GetArtFormsToHTML(Request.Form["langid"]);
             }
            if (Request.Form["reg"]!=null)
            {
                Regs();
            }
            if (Request.Form["isusername"] != null)
            {
                IsUserNameExists(Request.Form["isusername"], Request.Form["langid"]);
            }
            
        }
    }
    private void IsEmialExists(string emailUrl,string langid)
    {
        MemberBLL mb = new MemberBLL(int.Parse(langid));
        if (!mb.EmailIsExists(emailUrl))
        {
            Response.Write("1");
        }
        else
        {
            Response.Write("0");
        }
    }
    private void GetArtFormsToHTML(string langid)
    {
        ArtFormBLL afbll = new ArtFormBLL(int.Parse(langid));
        IList<ArtFormModel> lafm=afbll.GetArtFormModels();
        StringBuilder stb = new StringBuilder();
        foreach (ArtFormModel afm in lafm)
        {
            stb.AppendFormat("<li><input class='Arts0' name='artselect' id='{0}' type='checkbox' ' onclick='ccheck({0})' value='{0}'> {1}</li>", afm.ID.ToString(), afm.Name.Trim());
        }
        Response.Write(stb.ToString());
    }
    private void Regs()
    {


        
        MemberModel mm = new MemberModel();
        mm.Name=Request.Form["name"];
        mm.Email=Request.Form["email"];
        mm.Password = ECCIT.Security.MD5Crypto.GetMD5Hash(Request.Form["pass"]); 
        mm.Mobile=Request.Form["phone"];
        mm.MemberImg=Request.Form["headurl"];
        mm.Birthday=Request.Form["birthDay"];
        mm.FavoriteArtist = Request.Form["fave"];
        mm.Baidu=Request.Form["wblist"].Contains("Baidu")?"1":"0";
        mm.BBS=Request.Form["wblist"].Contains("BBS")?"1":"0";
        mm.Provice=Request.Form["city"].ToString().Split('/')[0] != "Select" ? Request.Form["city"].ToString().Split('/')[0] : "0";
        mm.City = Request.Form["city"].ToString().Split('/')[1] != "Select" ? Request.Form["city"].ToString().Split('/')[1] : "0";
        mm.DouBan=Request.Form["wblist"].Contains("DouBan")?"1":"0";
        mm.Bing=Request.Form["wblist"].Contains("Bing")?"1":"0";
        mm.Google=Request.Form["wblist"].Contains("Google")?"1":"0";
        mm.KaiXin=Request.Form["wblist"].Contains("KaiXin")?"1":"0";
        mm.NewsPaper=Request.Form["wblist"].Contains(" Newspaper")?"1":"0";
        mm.RenRen=Request.Form["wblist"].Contains("RenRen")?"1":"0";
        mm.Sohu=Request.Form["wblist"].Contains("Sohu")?"1":"0";
        mm.TengXun=Request.Form["wblist"].Contains("QQ")?"1":"0";
        mm.TengXunWeibo=Request.Form["wblist"].Contains("QQ")?"1":"0";
        mm.LanguageID=int.Parse( Request.Form["langid"]);
        mm.InsertTime = DateTime.Now;
        mm.LastLoginTime = DateTime.Now;

        MemberBLL mb=new MemberBLL (Convert.ToInt32( mm.LanguageID));
        ECCIT.ResultInfo rs = mb.Save(mm);
        Response.Write(rs.Result?"1":"0");
        int? artid = (rs.ReturnObject as MemberModel).ID;
        AccountController ac = new AccountController(Convert.ToInt32( mm.LanguageID));
        ac.ProcessLogin(Convert.ToInt32(artid));
        if (Request.Form["artslist"].Trim()!="")
        {
            string artlist = Request.Form["artslist"];
            string [] artArr= artlist.Substring(0).Split('|');
            MemberIntersetsMappingBLL mimbll = new MemberIntersetsMappingBLL( Convert.ToInt32( mm.LanguageID));
            MemberIntersetsMappingModel mimm = null;
            List<MemberIntersetsMappingModel> lmm = new List<MemberIntersetsMappingModel>();
            foreach (string  art in artArr)
            {
                if (art!="")
                {
                    mimm = new MemberIntersetsMappingModel();
                    mimm.CreateTime = DateTime.Now.ToShortDateString();
                    mimm.COL_MEMBER_ID = Convert.ToInt32(artid);
                    mimm.COL_INTERESTS_ID = int.Parse(art);
                    lmm.Add(mimm);
                
                }
               
              
                


            }
            mimbll.Save(lmm);
            

        }
    }
    private void IsUserNameExists(string username,string langid)
    {
        MemberBLL mb = new MemberBLL(int.Parse(langid));
        if (!mb.UserNameIsExists(username))
        {
            Response.Write("1");
        }
        else
        {
            Response.Write("0");
        }
    }
}